function change_checkbox(o,type){
   if(o.checked){
      document.getElementById(type).value=1;
   }else{
      document.getElementById(type).value=0;
   }
}
/**
 * 显示或隐藏分类关联的产品属性
 * @param int relation_type_id 产品分类ID
 * @return elements
 */
function add_relation_pros(relation_type_id) {
    var new_list = $('#type_' + relation_type_id);//获取当前分类对象
    var check = new_list.attr('checked');//获取分类对象的checked值

    if (check == 'checked') {//选中才添加属性
        $('.add_relation_pros_' + relation_type_id).remove();//先清除原先所有的元素
        //根据不同的分类添加不同的属性元素
        $('.wdw').append(props_html_arr_encode[relation_type_id]); //添加元素
    } else {
        //删除的分类中含有关联的属性，请取消后再删除分类
        var is_checked = 0;
        if (product_types_props[relation_type_id]) {
            for (var i = 0; i < product_types_props[relation_type_id].length; i++) {
                var children = product_types_props[relation_type_id][i]['children'];
                for (var j in children) {
                    var pro_id = children[j]['id'];
                    var input = $('.WSY_clorop input[value=' + pro_id + ']');
                    var check = input.prop('checked');
                    var WSY_clorop = input.parents('.WSY_clorop');
                    if (check) {
                        WSY_clorop.addClass('red_border');
                        is_checked++;
                    }
                }
            }
        }
        if (is_checked) {
            layer.alert('删除的分类中含有关联的属性，请取消后再删除分类');
            is_del = false;	
        }else{
            var html = $('.add_relation_pros_' + relation_type_id).html();
            $('.add_relation_pros_' + relation_type_id).remove();//当分类的checked为false 就移除 添加的属性选项
            is_del = true;	
        }
    }
}
/**
 * 产品属性复选框操作
 */
function chkPro() {
    var allpids = $("input[name='hidden_parent']");//获取属性复选框对象
    var parentArray = new Array();
    for (var i = 0, index = 0; i < allpids.length; i++) { //循环所有属性类型
        var parent_id = allpids[i].value;//父类ID
        var allprops = $("input[data_name='prop_" + parent_id + "']:checked");  //获取属性类型中选中的属性值
        if (allprops.length > 0) {//
            var childArray = new Array();
            for (var j = 0; j < allprops.length; j++) { //将每个选中的属性值信息拼接添加到数组
                var ckprop = allprops[j];
                var pid = $(ckprop).attr("data_pid");
                var text = $(ckprop).attr("data_text");
                var parent = $(ckprop).attr("data_parent");
                childArray[j] = pid + "," + text + "," + parent;
            }
            parentArray[index] = childArray;
            index++;
        }
    }
    setPropPrice(parentArray);
}
/**
 * 设置属性价格
 * 拼接属性价格传给接口
 */
function setPropPrice(propArrays) {
    // alert(1212)
    var cIndex = propArrays.length - 1;
    var counter = new Array();
    var str = getAppendText("", "", "");
    if (propArrays.length > 0) {
        var total = 1;
        for (var i = 0; i < propArrays.length; i++) {
            counter[i] = 0;
            total = total * propArrays[i].length;
        }
        for (var j = 0; j < total; j++) { //笛卡尔乘积
            var ids = "";
            var text = "";
            for (var index = 0; index < propArrays.length; index++) {
                var props = propArrays[index][counter[index]].split(",");
                if (props.length == 1) {
                    props[1] = $("#" + props[0]).val();
                }
                if (index > 0) {
                    ids += "_";
                    text += " - ";
                }
                ids += props[0];
                text += props[1];
            }
            str = getAppendText(str, ids, text);
            calcIndex();
        }
    }
    var divp = document.getElementById("div_proprices");
    str = str + "</div></div>";
    divp.innerHTML = str;
    if (propArrays.length > 0) {	//有属性才显示全部设置
        $('.all_do_prompt').show();
    }
    function calcIndex() {
        counter[cIndex]++;
        if (counter[cIndex] >= propArrays[cIndex].length) {
            counter[cIndex] = 0;
            cIndex--;
            if (cIndex >= 0) {
                calcIndex();
            }
            cIndex = propArrays.length - 1;
        }
    }
}


/**
 * 添加属性价格元素
 * str,
 * pid,  product_prices的proids  例如：15_16
 * text 
 */
function getAppendText(str, pid, text) {
    //console.log(str,pid,text);
    var modify = modify||{};
    var orgin_price = modify.orgin_price || 0;//原价
    var now_price = modify.now_price || 0;//现价
    var storenum = modify.storenum || 0;//库存
    var need_score = modify.need_score || 0;//所需积分
    var cost_price = modify.cost_price || 0;//供货价
    var foreign_mark = modify.foreign_mark || "";//外部标识
    var unit = modify.unit || "";//单位
    var weight = modify.weight || 0;//重量
    var for_price = modify.for_price || 0;//成本
    if (ppriceHash.contains(pid)) {
        var onprice = ppriceHash.items(pid);
        onprices = onprice.split("_");
        orgin_price = onprices[0];
        now_price = onprices[1];
        storenum = onprices[2];
        need_score = onprices[3];
        cost_price = onprices[4];
        foreign_mark = onprices[5];
        unit = onprices[6];
        weight = onprices[7];
        for_price = onprices[8];
    }
    if (pid != "") {
        str = str + '<ul class="WSY_bulkul01 pos_ul" pos="pid">';
        str = str + '<li class="WSY_bulkuli_red">' + text + '</li>';
        str = str + " <li class='orgin_price'>原价:￥<input type='text' name='pro[orgin_price][]' value='" + orgin_price + "' class='form_input num_check ' size='5' maxlength='10'></li>";
        str = str + " <li class='now_price'>现价:￥<input type='text' name='pro[now_price][]' value='" + now_price + "' class='form_input num_check calc_np' size='5' maxlength='10'></li>";
        str = str + " <li class='for_price'>成本:￥<input type='text' name='pro[for_price][]' value='" + for_price + "' class='form_input num_check calc_fp' size='5' maxlength='10'></li>";
        str = str + " <li class='base_price'>供货价:￥<input type='text' name='pro[cost_price][]' value='" + cost_price + "' class='form_input num_check calc_bp' size='5' maxlength='10'></li>";
        str = str + " <li>单位:<input type='text' name='pro[unit][]' value='" + unit + "' class='form_input' size='5' maxlength='10'></li>";
        str = str + " <li>重量:<input type='text' name='pro[weight][]' value='" + weight + "' class='form_input num_check ' size='5' maxlength='10'>KG</li>";
        str = str + " <li class='neet_score'>所需积分: <input type='text' name='pro[need_score][]' value='" + need_score + "' class='form_input num_check' size='5' maxlength='10'></li>";
        str = str + " <li class='store_num'>库存: <input type='text' name='pro[storenum][]' value='" + storenum + "' class='form_input num_check' size='5' maxlength='10'></li>";
        str = str + " <li class='del'>删除</li>";
        str = str + " <input type=hidden name='pro[proids][]' value='" + pid + "' />";
        str = str + "</ul>";
    } else {
        str = str + "<div class='WSY_bulkul01 main_form'>";
        str = str + "  <li class='orgin_price'>原价:￥<input type='text' name='orgin_price' value='"+orgin_price+"' class='form_input num_check' size='5' maxlength='10'> </li>";
        str = str + " <li class='now_price'>现价:￥<input type='text' name='now_price' value='"+now_price+"' class='form_input num_check calc_np' size='5' maxlength='10'> </li>";
        str = str + " <li class='for_price' > 成本:￥<input type='text' name='for_price' value='"+for_price+"' class='form_input num_check calc_fp' size='5' maxlength='10'> </li>";
        str = str + " <li class='base_price'>供货价:￥<input type='text' name='cost_price' value='"+cost_price+"' class='form_input num_check calc_bp' size='5' maxlength='10'></li>";
        str = str + " <li> 单位:<input type='text' name='unit' value='"+unit+"' class='form_input' size='5' maxlength='10'> </li>";
        str = str + " <li> 重量:<input type='text' name='weight' value='"+weight+"' class='form_input num_check ' size='5' maxlength='10'>KG</li>";
        str = str + " <li class='neet_score'> 所需积分: <input type='text' name='need_score' value='"+need_score+"' class='form_input num_check' size='5' maxlength='10'> </li>";
        str = str + " <li class='store_num'> 库存: <input type='text' name='storenum' value='"+storenum+"' class='form_input num_check' size='5' maxlength='10'> </li>";
        str = str + "</div>";
        str = str + '<div class="WSY_bulkul02box" style="width:95%">';
        str = str + '<div class="WSY_bulkul02">';
        str = str + '<span class="WSY_red">属性价格</span>';
    }
    return str;
}

function selectText(obj, id) {
    //alert(id);
    // 选择页签标识
    //$(obj).addClass('selected');
    $(obj).siblings().css('background-color', "");
    $(obj).css("background-color", "white");
    var obj_name = $(obj).attr("data-name");

    $('.text_box').css('display', "none");
    $('.' + obj_name).css('display', "block");
}

$(function () {
    var editor1 = UE.getEditor('container1');
    var editor2 = UE.getEditor('container2');
    var editor3 = UE.getEditor('container3');
    ppriceHash = new Hashtable();
    var msg = $(".msg");
    //点击开启下拉框
    $(".slide_btn").click(function () {
        if (msg.is(":visible")) {
            msg.fadeOut(500);
            $(this).hide();
            $(".slide_btn_grey").show();
        } else {
            msg.fadeIn(500);
        }
    });
    //关闭分类下拉框
    $(".slide_btn_grey").click(function () {
        if (msg.is(":visible")) {
            msg.fadeOut(500);
        } else {
            msg.fadeIn(500);
            $(this).hide();
            $(".slide_btn").show();
        }
    });
    //添加分类
    var all_array = "";//储存已选选项
    $(".msg").on("click", ".option_box .add-list", function () {
        var type_name = $(this).attr('type_name');
        var type_id = $(this).attr('data-type_id');
        if (all_array.indexOf(type_id) == -1) {
            var html = '<div class="new_list" id="type_' + type_id + '" data-type_id="' + type_id + '" checked="checked"><p>' + type_name + '</p><span class="del_type" data-type_id="' + type_id + '">x</span></div>';
            $('#f-box').append(html);//输出所选项
            all_array += type_id + ",";
            //console.log(all_array);
            add_relation_pros(type_id, '#type_' + type_id);	//显示关联属性
        } else {
            alert('已存在分类');
        }
    });
    //删除分类
    is_del = true;		//是否可以删除分类 ：true 可以 ；false否
    $("#f-box").on("click", ".new_list .del_type", function () {
        var type_id = $(this).attr('data-type_id');
        all_array = all_array.replace("," + type_id + ",", ",");
        all_array = all_array.replace(type_id + ",", ",");
        $(this).parent('.new_list').removeAttr('checked');
        //console.log(all_array);
        add_relation_pros(type_id, '#type_' + type_id);	//去掉关联属性
        if (is_del) {
            $(this).parent('.new_list').remove();
        }
    });
    //删除属性价格DIV
    $("#div_proprices").on("click", ".del", function () {
        $(this).parent().remove();
    });
});